package com.manor.main.activity

import android.content.Context
import android.content.Intent
import android.graphics.Color
import com.manor.common.Constants
import com.manor.common.activity.AbsActivity
import com.manor.common.utils.DpUtil
import com.manor.common.utils.WordUtil
import com.manor.main.R
import com.manor.main.adapter.CommonPagerAdapter
import com.manor.main.fragment.CharmFragment
import com.manor.main.fragment.VulgarFragment
import com.manor.main.http.MainHttpConsts
import com.manor.main.http.MainHttpUtil
import kotlinx.android.synthetic.main.view_main_list.*
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView

/**
 * 排行榜
 */
class RankingActivity : AbsActivity() {

    override fun getLayoutId(): Int {
        return R.layout.view_main_list
    }
    var type :Int = 0
    companion object {
        fun forward(context: Context, defaultPos: Int) {
            val intent = Intent(context, RankingActivity::class.java)
            intent.putExtra(Constants.RANK_DEFAULT, defaultPos)
            context.startActivity(intent)
        }
    }

    val titles = arrayOf( WordUtil.getString(R.string.profit_rank), WordUtil.getString(R.string.consume_rank))

    override fun main() {
        //        var cf: CharmFragment = CharmFragment()
//        var vf: VulgarFragment = VulgarFragment()
        btn_back.setOnClickListener({finish()})
        val pagerAdapter = CommonPagerAdapter(supportFragmentManager, CharmFragment(0), VulgarFragment(1))

        viewPager.setOffscreenPageLimit(0)//加载几页
        viewPager.setAdapter(pagerAdapter)
        val commonNavigator = CommonNavigator(this)
        commonNavigator.adapter = object : CommonNavigatorAdapter() {

            override fun getCount(): Int {
                return titles.size
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val simplePagerTitleView = ColorTransitionPagerTitleView(context)
                simplePagerTitleView.normalColor = Color.parseColor("#ffffff")
                simplePagerTitleView.selectedColor = Color.parseColor("#ffffff")
                simplePagerTitleView.setText(titles[index])
                simplePagerTitleView.textSize = 16f

                simplePagerTitleView.setPadding(DpUtil.dp2px(20), 0, DpUtil.dp2px(20), 0)
                simplePagerTitleView.setOnClickListener {
                    if (viewPager != null) {
                        viewPager.setCurrentItem(index)
                    }
                }
                return simplePagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator {
                val indicator = LinePagerIndicator(context)
                //  indicator.setBackgroundResource(R.mipmap.pic_tit);

                val borderWidth = UIUtil.dip2px(context, 1.0).toFloat()
                val lineHeight = UIUtil.dip2px(context, 32.0).toFloat()
                indicator.lineHeight = lineHeight
                indicator.roundRadius = 117f

                indicator.setBackgroundResource(R.drawable.bg_ranking)
                indicator.yOffset = borderWidth
                indicator.setColors(Color.parseColor("#FF8181"))
                return indicator
            }
        }
        indicator.setNavigator(commonNavigator)
        ViewPagerHelper.bind(indicator, viewPager)
        viewPager.setCurrentItem(0)

    }

    override fun onDestroy() {

        MainHttpUtil.cancel(MainHttpConsts.PROFIT_LIST)
        MainHttpUtil.cancel(MainHttpConsts.CONSUME_LIST)
        super.onDestroy()

    }

//    var mFragment: Fragment? = null
//    override fun main() {
//        var cf: CharmFragment = CharmFragment()
//        var vf: VulgarFragment = VulgarFragment()
//        tv_ml.setOnClickListener({
//            if(type ==1){
//                showFragment(cf)
//                type = 0
//                tv_ml.setBackgroundResource(R.drawable.bg_ranking_item)
//                tv_th.setBackgroundResource(0)
//            }
//        })
//        tv_th.setOnClickListener({
//            if(type ==0){
//                showFragment(vf)
//                type = 1
//                tv_ml.setBackgroundResource(0)
//                tv_th.setBackgroundResource(R.drawable.bg_ranking_item)
//            }
//
//        })
//        showFragment(cf)
//    }
//
//
//    private fun showFragment(fragment: Fragment) {
//        val transaction = this.supportFragmentManager.beginTransaction()
//        if (mFragment == null) {
//            transaction.add(R.id.frame, fragment).show(fragment).commit()
//            mFragment = fragment
//            return
//        }
//        if (mFragment !== fragment) {
//            if (!fragment.isAdded()) {
//                transaction.hide(mFragment!!).add(R.id.frame, fragment).show(fragment)
//            } else {
//                transaction.hide(mFragment!!).show(fragment)
//            }
//            transaction.commit()
//            mFragment = fragment
//        }
//    }

}
